package org.watchrecords.pages.crime;

import java.text.DateFormat;

import org.apache.commons.lang.StringUtils;
import org.apache.wicket.PageParameters;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.BookmarkablePageLink;
import org.apache.wicket.markup.html.link.ExternalLink;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.watchrecords.constants.Permission;
import org.watchrecords.dbo.Offender;
import org.watchrecords.managers.OffenderManager;
import org.watchrecords.pages.BasicPage;
import org.watchrecords.util.FormatHelper;

import com.google.inject.Inject;

public class ViewOffendersPage extends BasicPage {
    @Inject private OffenderManager offenderManager;
    
    public ViewOffendersPage() {
        super(null);
        final boolean canEdit = haveAnyPermission(Permission.EDIT_OFFENDER);
        final DateFormat format = FormatHelper.getDateFormat();
        WebMarkupContainer linkList = new WebMarkupContainer("linkList");

        add(linkList);
        
        if(!canEdit) {
            linkList.setVisible(false);
        }
        
        add(new ListView<Offender>("offenderList", offenderManager.getOffenders()) {
            /**
             * 
             */
            private static final long serialVersionUID = 1L;

            @Override
            protected void populateItem(ListItem<Offender> item) {
                final Offender off = item.getModelObject();
                
                String name = StringUtils.defaultString(off.getFullName());
                String url = "http://argentarchives.org/node/" + off.getArgentNode();
                ExternalLink archivesLink = new ExternalLink("name", url, name);
                
                item.add(archivesLink);
                archivesLink.setVisible(!name.isEmpty());
                archivesLink.setEnabled(off.getArgentNode() != null);
                
                item.add(new Label("created", off.getCreated() == null ? "" : format.format(off.getCreated())));
                item.add(new Label("updated", off.getUpdated() == null ? "" :format.format(off.getUpdated())));
                
                Link<Object> editLink = new BookmarkablePageLink<Object>("editLink", EditOffenderPage.class,
                        new PageParameters("offenderId=" + off.getId()));
                
                Link<Object> deleteLink = new Link<Object>("deleteLink") {
                    private static final long serialVersionUID = -99269614290037130L;

                    @Override
                    public void onClick() {
                        offenderManager.deleteOffender(off);
                        setResponsePage(ViewOffendersPage.class);
                    }
                };
                
                item.add(editLink);
                item.add(deleteLink);
                
                if (!canEdit) {
                    editLink.setVisible(false);
                    deleteLink.setVisible(false);
                }
                
                item.add(new BookmarkablePageLink<Object>("viewLink", ViewOffenderPage.class,
                        new PageParameters("offenderId=" + off.getId())));
            }
            
        });
    }
}
